<!-- 员工试用期考核表 -->
<template>
	<view class="page">
		<u--form
			labelPosition="top"
			:model="basicForm"
			ref="topForm"
			:rules="rules"
			labelWidth="auto"
			:labelStyle="{
				fontSize: '14px',
				color: '#B2B2B2',
				lineHight: '20px'
			}"
		>
			<view class="basic-style">
				<view class="basic-title basic-border">基本信息</view>
				
				<u-form-item
					label="员工姓名"
					prop="employeeId"
					:borderBottom="false"
					ref="item1"
				>
					<h-select-person-input
						v-model="basicForm.employeeId"
						style="width: 100%" 
						placeholder="请选择员工"
						border="surround"
						disabled
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
						objectField = 'basicForm.employeeId'
					></h-select-person-input>
				</u-form-item>
				<u-form-item
					label="人员编号"
					prop="employeeNo"
					:borderBottom="false"
					ref="item1"
				>
					<u--input
						v-model="basicForm.employeeNo"
						disabled
						placeholder="请选择人员编号"
						border="surround"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
					></u--input>
				</u-form-item>
				<u-form-item
					label="性别"
					prop="gender"
					:borderBottom="false"
					ref="item1"
				>
					<h-dict-select
						style="width: 100%" 
						v-model="basicForm.gender" 
						disabled
						placeholder="请选择性别" 
						dictName="gender" 
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
					></h-dict-select>
				</u-form-item>
				<u-form-item
					label="岗位"
					prop="positionId"
					:borderBottom="false"
					ref="item1"
				>
					<h-dict-select
						style="width: 100%" 
						v-model="basicForm.positionId" 
						disabled
						placeholder="请选择岗位" 
						dictName="sys_position,name,id,del_flag = 0 and audit_status=2" 
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
					></h-dict-select>
				</u-form-item>
				<u-form-item
					label="入职时间"
					prop="partTime"
					:borderBottom="false"
					ref="item1"
				>
					<u--input
						v-model="basicForm.partTime"
						disabled
						placeholder="请选择入职时间"
						border="surround"
						suffixIcon="calendar-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
					></u--input>
				</u-form-item>
				
				<u-form-item
					label="试用期满时间"
					prop="endTime"
					:borderBottom="false"
					ref="item1"
					required
					@click="onEndTime"
				>
					<u--input
						v-model="basicForm.endTime"
						:disabled="true"
						:disabledColor="disableSubmit?'#f5f7fa':'#ffffff'"
						placeholder="请选择试用期满时间"
						border="surround"
						suffixIcon="calendar-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
					></u--input>
				</u-form-item>
			</view>
		
			<!-- 评测信息 -->
			<view class="basic-style">
				<view class="basic-title basic-border">评测信息</view>
				<view v-for="(item, key) in question" :key="key" class="basic-padding">
					<view class="basic-title">{{item.title}}</view>
					<view class="" v-for="(val, index) in item.children" :key="index">
						<view class="info-title">{{(index+1)+'、'+val.title}}</view>
						<u-form-item
							label=""
							:prop="question[key].children[index].code"
							:borderBottom="false"
						>
							<view class="info">
								 <u-radio-group
										v-model="basicForm.question[key].children[index].value"
										placement="column"
										@change="groupChange($event,key,index)"
										labelColor="#171717"
										shape="square"
										:disabled="disableSubmit"
									>
										<u-radio
											labelSize="14"
											:customStyle="{marginBottom: '8px'}"
											v-for="(value, inde) in val.children"
											:key="inde"
											:label="value.title"
											:name="value.code"
										>
										</u-radio>
									</u-radio-group>
							</view>
						</u-form-item>
					</view>
				</view>
			</view>
			
			<view class="basic-style">
				<view class="basic-title basic-border">评测结果</view>
				<view class="result-style basic-padding">
					说明：A优秀 ：95分以上可提前2个月转正；B优良：90分以上可提前1个月转正；C较好：75（含）-90分可正常转正；D一般：60-75分延长试用；E较差：60分以下终止试用。
				</view>
				<u-form-item
					label="考核得分"
					prop="evaluatingResult"
					:borderBottom="false"
					ref="item1"
					required
				>
					<u-input
						placeholder="请计算考核得分"
						v-model="basicForm.evaluatingResult"
						border="surround"
						disabled
					>
						<template slot="suffix">
							<view class="" style="color: red; border-left: 1px solid #ccc; padding-left: 10rpx;">{{gradeResult}}</view>
						</template>
					</u-input>
					<view  style="margin-left: 5%;" @click="calculate">计算得分</view>
				</u-form-item>
				<u-form-item
					label="评测人"
					prop="evaluatingEmployee"
					:borderBottom="false"
					ref="item1"
				>
					<h-select-person-input
						disabled
						style="width: 100%"
						v-model="basicForm.evaluatingEmployee"
						placeholder="请选择评测人"
						border="surround"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
						objectField="basicForm.evaluatingEmployee"
					></h-select-person-input>
				</u-form-item>
				<u-form-item
					label="评测时间"
					prop="evaluatingTime"
					:borderBottom="false"
					ref="item1"
				>
					<u--input
						v-model="basicForm.evaluatingTime"
						disabled
						placeholder="请选择评测时间"
						border="surround"
						suffixIcon="calendar-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
					></u--input>
				</u-form-item>
				
				<u-form-item
					label="评价"
					prop="evaluationPeriod"
					:borderBottom="false"
					ref="item1"
				>
					<u--textarea
						:disabled="disableSubmit"
						v-model="basicForm.evaluationPeriod"
						:confirmType="null"
						count
						placeholder="请填写评价"
						autoHeight
						:maxlength="500"
						style="min-height: 200rpx"
					></u--textarea>
				</u-form-item>
			</view>
		</u--form>
		<!-- 附件 -->
		<view class="basic-style">
			<view class="basic-title basic-border">附件信息</view>
			<view class="" style="width: 100%;">
				<h-upload-file v-model="basicForm.attachment" :isIcon="false" v-if="!disableSubmit"></h-upload-file>
				<file-list v-if="basicForm.attachment" v-model="basicForm.attachment" :isDelete="!disableSubmit"></file-list>
			</view>
		</view>
		<view class="" style="padding: 20rpx; display: flex;" v-if="!disableSubmit">
			<u-button style="margin-right:20rpx;" type="primary" text="保存" @click="onSubmit(true)"></u-button>
			<u-button type="primary" text="保存草稿" @click="onSubmit(false)"></u-button>
		</view>
		<u-datetime-picker
			:show="timeShow"
			v-model="timeValue"
			mode="date"
			:minDate="entryTime"
			closeOnClickOverlay
			@close="timeShow = false"
			@cancel="timeShow = false"
			@confirm="handleTime"
		></u-datetime-picker>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
import {
		queryByIdHrRoot, // 考核列表
		queryByIdHrRegular, // 员工考核表详情
		queryByIdEmployeeId
	} from '@/api/index.js'
import fileList from './components/fileList.vue'
import moment from 'moment'
import {
		mapState
	} from 'vuex';
export default {
	components: {
		fileList
	},
	data() {
		return {
			basicForm: {
				employeeId: '', // 员工姓名
				employeeNo: '', // 人员编号
				gender: '', // 性别
				positionId: '', // 岗位
				partTime: '', // 入职时间
				endTime: '', // 试用期满时间
				evaluatingEmployee: '',
				evaluatingTime: '',
				evaluatingResult: ''
			},
			timeValue: '',
			entryTime: moment().subtract(20, "years").format("x") - 0, // 试用期时间不小于此时间
			question: [], // 列表
			timeShow: false,
			rules: {
				endTime: {
					required: true,
					message: '请选择试用期满时间',
					trigger: 'change'
				},
				evaluatingResult: {
					required: true,
					message: '请点击计算得分按钮',
					trigger: ['blur', 'change']
				}
			},
			gradeResult: '分',
			eventChannel: null,
			disableSubmit: false
		};
	},
	onLoad(options) {
		// this.initData()
		var _that = this
		const eventChannel = this.eventChannel = this.getOpenerEventChannel();
		eventChannel.on('sendMessage', function(data) {
			const result = data.data
			_that.disableSubmit = data.disableSubmit
			_that.entryTime = Number(moment(result.entryTime).format('x'))
			if(result.regularDetail){
				_that.edit(result.regularDetail)
			} else {
				_that.getDetail(result)
			}
		})
	},
	methods: {
		...mapState(['userInfo']),
		handleTime(value){
			if(value.value){
				this.$set(this.basicForm,'endTime',moment(value.value).format('YYYY-MM-DD'))
				this.timeShow = false
			}
		},
		
		// 点击选择时间
		onEndTime () {
			if (this.disableSubmit) {
				return false
			}
			this.timeShow = true
		},
		// 计算得分
		calculate(){
			if(this.disableSubmit) return
			this.rules.evaluatingResult = {}
			this.rules.endTime = {}
			this.$refs.topForm.validate().then(res => {
				if(res){
					let mark=0;
					let result="";
					let evaluating=this.basicForm.evaluating;
					for(var key in evaluating){
						let str=evaluating[key]
						let level=str.substring(11,12);
						if(level=='1'){
							mark+=10;
						}else if(level=='2'){
							mark+=9;
						}else if(level=='3'){
							mark+=7;
						}else if(level=='4'){
							mark+=6;
						}else if(level=='5'){
							mark+=5;
						}
					}
					if(mark>=95){
						result="A优秀"
					}else if(mark>=90){
						result="B优良"
					}else if(mark>=75){
						result="C较好"
					}else if(mark>=60){
						result="D一般"
					}else{
						result="E较差"
					}
					this.$set(this.basicForm,'evaluatingResult',mark);
					this.gradeResult = result? result: ''
				}
			}).catch(errors => {
				console.error(errors)
			})
		},
		judgeMark(){
			if(this.basicForm.evaluatingResult){
				let mark = this.basicForm.evaluatingResult;
				let result='';
				if(mark>=95){
					result="A优秀"
				}else if(mark>90){
					result="B优良"
				}else if(mark>=75){
					result="C较好"
				}else if(mark>60){
					result="D一般"
				}else{
					result="E较差"
				}
				this.gradeResult = result ? result :''
			} else {
				this.gradeResult = '分'
			}
		},
		add(data){
			this.$set(this.basicForm,'evaluating',{})
			this.$set(this.basicForm,'employeeId',data.employeeId)
			this.$set(this.basicForm,'employeeNo',data.employeeNo)
			this.$set(this.basicForm,'gender',data.gender)
			this.$set(this.basicForm,'positionId',data.positionId)
			this.$set(this.basicForm,'partTime',data.entryTime)
			this.$set(this.basicForm,'endTime',data.endTime)
			this.$set(this.basicForm,'evaluatingEmployee',this.userInfo().empId)
			this.$set(this.basicForm,'evaluatingTime',moment().format('YYYY-MM-DD HH:mm:ss'))
			this.handleEvaluating(data)
			this.judgeMark()
		},
		edit(record){
			this.basicForm = Object.assign({},record)
			this.handleEvaluating(record)
			this.judgeMark()
		},
		//初始化问题
		initData () {
			let params = {
				async: false,
				pcode: 'A02',
				removeCode:''
			}
			queryByIdHrRoot(params).then(res => {
				let question = res.result;
				this.question = question
				this.basicForm.question = question
				for(var z=0;z<question.length;z++){
					let item=question[z].children;
					for(var i=0;i<item.length;i++){
						let temp = item[i].title;
						this.$set(this.rules, question[z].children[i].code, { required: true,message: '请选择'+temp+"!",trigger: 'change'});
					}
				}
			}).catch(err => {
				// _this.isClick = false
			})
		},
		//处理评测数据
		handleEvaluating (record) {
			let params = {
				async: false,
				pcode: 'A02',
				removeCode:''
			}
			queryByIdHrRoot(params).then(res => {
				let question = res.result
				for(var key=0;key<question.length;key++){
					let item = question[key].children;
					for(var index=0;index<item.length;index++){
						let temp = item[index].title;
						if(record.evaluating){
							let evaluating = record.evaluating;
							item[index].value = evaluating[item[index].code];
							this.$set(this.basicForm,item[index].code,evaluating[item[index].code]);
						}
						this.$set(this.rules, question[key].children[index].code,{ required: true,message: '请选择'+temp+"!",trigger: 'change'});
					}
					this.question = question;
					this.$set(this.basicForm,'question',question);
				}
			}).catch(err => {
				// _this.isClick = false
			})
		},
		// 获取详情
		getDetail (record) {
			let data = Object.assign({},record)
			queryByIdEmployeeId({
				employeeId: record.employeeId
			}).then(res => {
				if(res.success){
					if(res.result){
						let endTime = record.entryTime;
						endTime = moment(endTime).add(res.result.probationership, 'months').format('YYYY-MM-DD');
						data.endTime = endTime;
					}
					this.add(data)
				}
			}).catch(err => {
				
			})
		},
		groupChange(data,key,index) {
			this.$set(this.basicForm,this.question[key].children[index].code,data);
			let code = this.question[key].children[index].code;
			let evaluating = Object.assign({},this.basicForm.evaluating);
			evaluating[code] = data;
			this.basicForm.evaluating = evaluating;
			this.$set(this.basicForm,"evaluatingResult",'');
			this.gradeResult='分'
		},
		onSubmit(flag){
			if (!flag) {
				this.eventChannel.emit('acceptMessage', { data: this.basicForm,flag });
				this.$refs.uToast.show({
					type: 'success',
					message: "保存草稿成功",
					complete() {
						uni.navigateBack()
					}
				})
				
			} else {
				this.rules.evaluatingResult = {
					required: true,
					message: '请点击计算得分按钮',
				}
				this.rules.endTime = {
					required: true,
					message: '请选择试用期满时间',
					trigger: 'change'
				}
				this.$refs.topForm.validate().then(res => {
					if(res){
						this.eventChannel.emit('acceptMessage', { data: this.basicForm,flag });
						uni.navigateBack()
					}
				})
			}
			
		}
	}
};
</script>

<style lang="scss" scoped>
/deep/ .u-form-item__body{
	padding: 20rpx 0 4rpx;
}
// 评测信息
.info-title{
	font-size: 28rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #171717;
	line-height: 40rpx;
	padding: 16rpx 0 0;
}

// 评测结果
.result-style{
	font-size: 24rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #B2B2B2;
}
</style>